package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;

import com.google.gson.Gson;

import pojo.BaseDataPojo;
import pojo.Book;
import pojo.College;
import pojo.CollegeBrochure;
import service.CollegeService;
import service.MallService;
import serviceImpl.CollegeServiceImpl;
import serviceImpl.MallServiceImpl;


@WebServlet("/CollegeServlet")
public class CollegeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public CollegeServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mark = request.getParameter("mark");
		switch(mark) {
		
		case "getcolleges":   // 获取商城首页轮播图图片
			getcolleges(request,response);
			break;
		case "getcollegeBrochure":   // 获取商城首页轮播图图片
			getcollegeBrochure(request,response);
			break;
		}
	}


	private void getcollegeBrochure(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int collegeid = Integer.parseInt(request.getParameter("collegeid"));
	
		CollegeService cs = new CollegeServiceImpl();
		ArrayList<CollegeBrochure> cbList = new ArrayList<CollegeBrochure>();
		cbList = cs.getcollegeBrochure(collegeid);
		Map<String,ArrayList<CollegeBrochure>> map = new HashedMap<>();
		map.put("cbList",cbList);
		//System.out.println("bList:"+bList);
		if(cbList!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<CollegeBrochure>>>("成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<CollegeBrochure>>>("空或失败", false, map)));
		}
		
	}


	private void getcolleges(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
	
		CollegeService cs = new CollegeServiceImpl();
		ArrayList<College> cList = new ArrayList<College>();
		cList = cs.getcolleges();
		Map<String,ArrayList<College>> map = new HashedMap<>();
		map.put("cList",cList);
		//System.out.println("bList:"+bList);
		if(cList!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<College>>>("成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<College>>>("空或失败", false, map)));
		}
		
	}

}
